import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { VantResolver } from '@vant/auto-import-resolver'
import { createHtmlPlugin } from 'vite-plugin-html'
import cdn from 'vite-plugin-cdn-import'
import postcssPxToViewPort from 'postcss-px-to-viewport'

const getViteEnv = (mode, target) => {
  return loadEnv(mode, process.cwd())[target]
};

// https://vite.dev/config/
export default defineConfig(({mode}) => {
  return {
    base: '/jdbfx',
    server: {
      host: '0.0.0.0'
    },
    plugins: [
      vue(),
      // vueDevTools(),
      AutoImport({
        resolvers: [VantResolver()],
      }),
      Components({
        resolvers: [VantResolver()],
      }),
      createHtmlPlugin({
        /**
         * 需要注入 index.html ejs 模版的数据
         */
        inject: {
          data: {
            title: getViteEnv(mode, "VITE_APP_TITLE"),
          },
        },
      }),
      cdn({
        modules: [
          {
            name: 'vconsole',
            var: 'VConsole',
            path: 'https://cdn.jiakaojingling.com/static/js/vconsole@3.15.1/vconsole.min.js',
          },
          // {
          //   name: 'vue',
          //   var: 'Vue',
          //   path: `https://cdn.jiakaojingling.com/static/js/vue@3.5.13/vue.global.prod.js`,
          // },
          // {
          //   name: 'vue-router',
          //   var: 'VueRouter',
          //   path: 'https://cdn.jiakaojingling.com/static/js/vue-router@4.5.1/vue-router.global.prod.js',
          // },
          // {
          //   name: 'vue-demi',
          //   var: 'VueDemi',
          //   path: 'https://cdn.jiakaojingling.com/static/js/vue-demi@0.14.10/index.iife.js',
          // },
          // {
          //   name: 'pinia',
          //   var: 'Pinia',
          //   path: 'https://cdn.jiakaojingling.com/static/js/pinia@2.3.0/pinia.iife.prod.js',
          // },
          {
            name: 'axios',
            var: 'axios',
            path: 'https://cdn.jiakaojingling.com/static/js/axios@1.7.9/axios.min.js',
          },
          // {
          //   name: 'vant',
          //   var: 'vant',
          //   path: 'https://cdn.jiakaojingling.com/static/js/vant@4.9.20/vant.min.js',
          //   css: 'https://cdn.jiakaojingling.com/static/js/vant@4.9.20/index.css\n',
          // },
        ],
      }),
    ],
    css:{
      postcss:{
        plugins:[
          postcssPxToViewPort({
            viewportWidth: 375,
          })
        ]
      }
    },
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
    },
  }
})
